import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/collection_model/get_qualification_list_model.dart';
import 'package:yysk_app/model/common_model/mine_points_detail_model.dart';
import 'package:yysk_app/net/api/product_api.dart';
import 'package:yysk_app/routes/global_controller.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/widget/refresh_and_scroll_widget.dart';
import 'package:yysk_app/net/api/api_url.dart';
import 'package:yysk_app/net/http.dart';

class CheckEligibilityPage extends StatefulWidget {
  static const path = '/checkEligibility';
  const CheckEligibilityPage({super.key});
  @override
  State<StatefulWidget> createState() => _CheckEligibilityPage();
}

class _CheckEligibilityPage extends State<CheckEligibilityPage> {
  final ScrollController scrollController = ScrollController();
  final GlobalController globalController = Get.find();

  var pageNum = 1;
  var pageSize = 20;
  final list = RxList<GetQualificationListModel>([]);
  final pointsId = RxString('0');
  final pointsImageUrl = RxString('');
  var id = '0';
  var processType = '';
  @override
  void initState() {
    super.initState();
    final params = Get.parameters;
    final args = Get.arguments ?? {};
    id = params['id'] ?? args['id'];
    processType = params['processType'] ?? args['processType'];

    getPointListData();
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  Size get preferredSize => const Size.fromHeight(kToolbarHeight);

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: Obx(
              () => Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(height: top),
                  CustomAppBar2(title: '查看资格', context: context),
                  hiSpace(height: 20.px),

                  Container(
                    width: context.width,
                    padding: EdgeInsets.all(16.px),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(18.px),
                        topRight: Radius.circular(18.px),
                      ),
                      gradient: LinearGradient(
                        colors: [
                          Color.fromRGBO(31, 33, 38, 1),
                          Color.fromRGBO(30, 33, 38, 0),
                        ],
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                      ),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Text(
                          '持有条件',
                          style: TextStyle(
                            fontSize: 13.px,
                            color: AppColors.backgroundColorGrey,
                          ),
                          textAlign: TextAlign.left,
                        ),
                        Row(
                          children: [
                            Text(
                              '享受数量',
                              style: TextStyle(
                                fontSize: 13.px,
                                color: AppColors.backgroundColorGrey,
                              ),
                              textAlign: TextAlign.left,
                            ),
                            SizedBox(width: 20.px),
                            Text(
                              '是否占用公售数量',
                              style: TextStyle(
                                fontSize: 13.px,
                                color: AppColors.backgroundColorGrey,
                              ),
                              textAlign: TextAlign.left,
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.fromLTRB(0.px, 0.px, 0.px, 0),
                      child: RefreshAndScrollWidget(
                        onRefresh: _handleRefresh,
                        scrollController: scrollController,
                        itemBuilder: (item, index) => _listItem(item),
                        itemCount: list.length,
                        items: list,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  void _handleRefresh() async {
    // 获取数据
    getPointListData();
  }

  getPointListData({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<List<GetQualificationListModel>>(
        ProductApi.getQualification,
        params: {'id': id, 'processType': processType},
        method: Method.post,
        success: (result) {
          List<GetQualificationListModel> items = result.data ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            list.clear();
          }
          list.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }

  _listItem(GetQualificationListModel item) {
    return Container(
      padding: EdgeInsets.fromLTRB(12.px, 14.px, 12.px, 0.px),
      decoration: BoxDecoration(color: AppColors.backgroundColorBlackLight),
      child: Container(
        padding: EdgeInsets.fromLTRB(0.px, 0.px, 0.px, 14.px),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 0.5.px,
              color: AppColors.backgroundColorGrey,
            ),
          ),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            LazyImage(
              imageUrl: '${item.conditionImageUrl}',
              width: 28.px,
              height: 28.px,
              boxFit: BoxFit.cover,
              borderRadius: 6.px,
            ),
            hiSpace(width: 10.px),
            SizedBox(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    width: 140.px,
                    child: RichText(
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      text: TextSpan(
                        children: <TextSpan>[
                          TextSpan(
                            text: '${item.conditionName}',
                            style: TextStyle(
                              color: AppColors.fontColor,
                              fontSize: 14.px,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  SizedBox(width: 160.px),
                ],
              ),
            ),
            hiSpace(width: 3.px),
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    '+${'${item.count}'}',
                    style: TextStyle(
                      color: AppColors.fontColorGray99,
                      fontSize: 14.px,
                      overflow: TextOverflow.ellipsis,
                    ),
                    textAlign: TextAlign.right,
                  ),
                  Text(
                    item.occupyPublicSale == true ? '是' : '否',
                    style: TextStyle(
                      color: AppColors.fontColorGray99,
                      fontSize: 14.px,
                      overflow: TextOverflow.ellipsis,
                    ),
                    textAlign: TextAlign.right,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
